package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.POJO.Address;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.MapService;
import org.omg.IOP.Encoding;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.beans.Encoder;
import java.util.Base64;
import java.util.List;

@RestController
@RequestMapping("/map")
public class MapController {

    @Reference
    private MapService mapService;

    @RequestMapping("/findAddress")
    public Result findAddress(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult=mapService.findAddress(queryPageBean);
            return new Result(true,"",pageResult);
        } catch (Exception e) {
            return new Result(false,"查询地址错误");
        }
    }

    @RequestMapping("/deleteAddress")
    public Result deleteAddress(String id){
        try {
            mapService.deleteAddress(id);
            return new Result(true,"删除地址正确");
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            return new Result(false,"删除地址错误");
        }
    }

    @RequestMapping("/getAddress")
    public Result getAddress(){
        try {
            List<Address>list =mapService.getAddress();
            return new Result(true,"",list);
        } catch (Exception e) {
            return new Result(false,"查询地址错误");
        }
    }

    @RequestMapping("/add")
    public Result add(String longitude,String latitude,String address){
        try {
            String rname = new String(address.getBytes("iso-8859-1"), "utf-8");
            mapService.addAddress(longitude,latitude,rname);
            return new Result(true,"");
        } catch (Exception e) {
            return new Result(false,"查询地址错误");
        }
    }
}
